package com.sxpcwlkj.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxpcwlkj.common.utils.MapstructUtil;
import com.sxpcwlkj.framework.entity.page.PageQuery;
import com.sxpcwlkj.framework.entity.page.TableDataInfo;
import com.sxpcwlkj.system.entity.SysOss;
import com.sxpcwlkj.system.entity.bo.SysOssBo;
import com.sxpcwlkj.system.entity.vo.SysOssVo;
import com.sxpcwlkj.system.mapper.SysOssMapper;
import com.sxpcwlkj.system.service.SysOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * @Description TODO
 * @Author sxpcwlkj
 * @Version v1.0.0
 */
@RequiredArgsConstructor
@Service("sysOss")
@Slf4j
public class SysOssServiceImpl implements SysOssService {

    private final SysOssMapper baseMapper;
    @Override
    public TableDataInfo<SysOssVo> selectPageList(SysOssBo bo, PageQuery pageQuery) {
        Page<SysOssVo> page = baseMapper.selectByPage(pageQuery.build(), this.buildQueryWrapper(bo));

        return TableDataInfo.build(page);
    }

    private Wrapper<SysOss> buildQueryWrapper(SysOssBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<SysOss> wrapper = Wrappers.query();
        //角色启用

        wrapper.orderByAsc("sort");
        return wrapper;
    }

    @Override
    public SysOssVo selectVoById(Long id) {
        SysOssVo vo = baseMapper.selectVoById(id);
        if(vo!=null){
            try {
                URL url = new URL(vo.getUrl());
                InputStream inputStream = url.openConnection().getInputStream();
                BufferedImage bufferedImage = ImageIO.read(inputStream);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
                byte[] bytes = byteArrayOutputStream.toByteArray();

                Base64 base64Encoder = new Base64();
                String base64String = base64Encoder.encodeToString(bytes);
                vo.setBase64(base64String);
            }catch (Exception e){
                vo.setBase64("");
            }

        }
        return vo;
    }

    @Override
    public Boolean updateById(SysOssBo bo) {
        int row=0;
        SysOss convert = MapstructUtil.convert(bo, SysOss.class);
        return baseMapper.updateById(convert)>0;
    }

    @Override
    public Boolean insert(SysOssBo bo) {
        SysOss convert = MapstructUtil.convert(bo, SysOss.class);
        return baseMapper.insert(convert)>0;
    }

    @Override
    public Boolean deleteById(Long id) {
        return baseMapper.deleteById(id)>0;
    }
}
